package de.playmee.handler;

import java.util.ArrayList;
import java.util.LinkedList;

import com.google.android.maps.GeoPoint;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;
import de.playmee.Channel;
import de.playmee.geo.Region;

public class RegionHandler extends DatabaseHandler {
	public static String TAG = "RegionHandler";
	
	public static final String KEY_ID = "_id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_LATITUDE = "latitude";
	public static final String KEY_LONGITUDE = "longitude";
	public static final String KEY_RADIUS = "radius";
	public static final String KEY_CHANNEL = "channel";
	
	public static final String DATABASE_TABLE = "region";
	
	public static final String DATABASE_CREATE = "create table " + DATABASE_TABLE
			+ " (" + KEY_ID + " integer primary key autoincrement, "
			+ KEY_TITLE + " text not null, "
			+ KEY_LATITUDE + " integer not null, "
			+ KEY_LONGITUDE + " integer not null, "
			+ KEY_RADIUS + " float not null, "
			+ KEY_CHANNEL + " long not null"
			+ ");";
	
	private ChannelHandler cHandler;
	
	public RegionHandler(DatabaseHelper dbHelper, ChannelHandler cHandler) {
		super(dbHelper);
		this.cHandler = cHandler;
		// now available: dbHelper, db, DELIMITER
	}

	public ArrayList<Region> loadRegions(String selection, String groupBy, String having, String orderBy) {
		Log.i(TAG, "Querying all regions...");
		ArrayList<Region> regions = new ArrayList<Region>();
		Cursor cur = db.query(DATABASE_TABLE, null, selection, null, groupBy, having, orderBy);
		if (cur != null && cur.moveToFirst()) {
			do {
				Region region = createRegion(cur);
				regions.add(region);
			} while (cur.moveToNext());
		}
		
		return regions;
	}
	
	public Region loadRegion(long _id) {
		Log.i(TAG, "Queriying particular region (" + _id + ")...");
		Region region = null;
		Cursor cur = db.query(true, DATABASE_TABLE, null, KEY_ID
				+ "=" + _id, null, null, null, null, null);
		if (cur != null && cur.moveToFirst()) {
			region = createRegion(cur);
		}
		
		return region;
	}
	
	public boolean insertRegion(Region region) {
		Log.i(TAG, "Inserting region (" + region.getTitle() + ")...");
		ContentValues initialValues = getContentValues(region);
		long id = db.insert(DATABASE_TABLE, null, initialValues);
		if (id != -1) {
			region.setId(id);
			return true;
		}
		return false;
	}
	
	public boolean deleteRegion(long _id) {
		Log.i(TAG, "Deleting region " + _id + "...");
		return db.delete(DATABASE_TABLE, KEY_ID + "=" + _id, null) > 0;
	}
	
	public boolean updateRegion(Region region) {
		Log.i(TAG, "Updating region (" + region.getId() + ")...");
		ContentValues args = getContentValues(region);
		return db.update(DATABASE_TABLE, args, KEY_ID + "=" + region.getId(), null) > 0;
	}
	
	public Region createRegion(Cursor cur) {
		// get column indices
		int idIndex = cur.getColumnIndex(KEY_ID);
		int titleIndex = cur.getColumnIndex(KEY_TITLE);
		int latitudeIndex = cur.getColumnIndex(KEY_LATITUDE);
		int longitudeIndex = cur.getColumnIndex(KEY_LONGITUDE);
		int radiusIndex = cur.getColumnIndex(KEY_RADIUS);
		int channelIndex = cur.getColumnIndex(KEY_CHANNEL);
		
		
		// get values of the row
		long id = cur.getLong(idIndex);
		String title = cur.getString(titleIndex);
		int latitude = cur.getInt(latitudeIndex);
		int longitude = cur.getInt(longitudeIndex);
		float radius = cur.getFloat(radiusIndex);
		//String priority = cur.getString(priorityIndex);
		long channelId = cur.getLong(channelIndex);
		
		// create region and define attributes
		Region region = new Region(new GeoPoint(latitude, longitude), radius, title);
		region.setId(id);
		//region.setPriorityList(parseStringToChannelList(priority));
		region.setChannel(cHandler.loadChannel(channelId));
		
		return region;
	}
	
	private ContentValues getContentValues(Region region) {
		ContentValues res = new ContentValues();
		res.put(KEY_TITLE, region.getTitle());
		res.put(KEY_LATITUDE, region.getCenter().getLatitudeE6());
		res.put(KEY_LONGITUDE, region.getCenter().getLongitudeE6());
		res.put(KEY_RADIUS, region.getMeters());
		res.put(KEY_CHANNEL, region.getChannel().getId());
		return res;
	}
}
